﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Documents
//  File name:	ListFormsSource.cs
//  Developer:	Solomatov Igor
//  Created:	16.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Generic;
using Smart.Common;
using Smart.Common.Entities;
using Smart.Common.Forms;
//using Smart.Documents.Scopes;

namespace Smart.Documents.Services
{
	/// <summary>
	/// SharePoint list item forms
	/// </summary>
	public class ListFormsSource : FormsSource
	{
		/// <summary>
		/// SharePoint list item forms
		/// </summary>
		public ListFormsSource(FormsContext ctx)
			: base(ctx)
		{
		}

		/// <summary>
		/// Detect metadata for entity
		/// </summary>
		public override MetaList DetectMetaList()
		{
			var entity = Ctx.Entity.Source as Entity;

			if (entity == null)
				throw new ArgumentException("Ctx.Entity");

			return (MetaList)entity.List.Meta;
		}

		/// <summary>
		/// Load entity values into context
		/// </summary>
		public override void LoadEntityValuesToCtx()
		{
			var entity = Ctx.Entity.Source as Entity;

			if (entity == null)
				throw new ArgumentException("Ctx.Entity");

			var web = SPH.GetWeb();

			var culture =
				web != null
					? web.UICulture
					: System.Threading.Thread.CurrentThread.CurrentUICulture;

			var d = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

			var webEntity = Ctx.Entity.Source as WebEntity;

			if (webEntity != null)
			{
				webEntity.GetItem().GetValues(d);
			}
			else
			{
				entity.GetEntityValues(d);
			}

			d.GetUserValues(Ctx.Values, culture);
		}

		/// <summary>
		/// Save entity values from context
		/// </summary>
		public override bool SaveEntityValuesFromCtx()
		{
			/*
			var entity = Ctx.Entity.Source as Entity;

			if (entity == null)
				throw new ArgumentException("Ctx.Entity");

			return
				entity.SetEntityValues(Ctx.Values)
				&& entity.Save();
			 */

			return true;
		}

		/// <summary>
		/// Detect form type (using context)
		/// </summary>
		public override string DetectFormsType()
		{
			var entity = Ctx.Entity.Source as WebEntity;

			if (entity == null)
				throw new ArgumentException("Ctx.Entity");

			throw new NotImplementedException();

			/*
						using (var documents = SPScopes.Documents)
						{
							var dt = documents.DocumentTypes.Query
								.Where.Eq(Const.Guids.DocumentType.DocumentContentTypeId.ToString(), Ctx.Entity.IsNew ? Ctx.Entity.ContentTypeId : entity.ContentTypeId.Parent.ToString())
								.Executor.ExecuteOne();

							if (dt == null)
								return null;

							var dfs = documents.GetFormsByType(dt.ID, ((MetaWebList)entity.List.Meta).Url);

							if (dfs == null || dfs.Count != 1)
								return null;

							return dfs[0].Title;
						}
			*/
		}

		/// <summary>
		/// Load form definition by <paramref name="formsTypeName"/>
		/// </summary>
		public override object LoadFormsDefinition(string formsTypeName)
		{
			throw new NotImplementedException();

			/*
						using (var documents = SPScopes.Documents)
						{
							var form = documents.GetForm(formsTypeName);

							return form == null ? null : form.Definition;
						}
			*/
		}
	}
}